<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Message extends MY_Controller {
	
	function __construct(){
		parent::__construct();
		$user = $this->session->userdata('zjy_user');
		if(!isset($user['user_id'])){
			$str = $_SERVER['REQUEST_URI'];
			$this->js->go('/login.html');
		}elseif(!$user['user_id']){
			$this->js->go('/login.html');
		}
	}
	
	function index(){
		
	}
	
	function received(){
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$page = intval($this->input->get('per_page'));;
			$user_id = $user['user_id'];
			$data = array();
			$data = $this->get_user_info('message_sjx','message','invite');
			$this->load->model('Message_model', 'message');
			$msg = $this->message->receive_box($user_id,$page);
			//$no_read_count = $this->message->no_read_count($user_id);
			//var_dump($msg);exit;
			
			$this->load->library('pagination');
			$config['base_url'] = '/message/received.html?';
			$config['total_rows'] = $msg['count'];
			$config['per_page'] = USER_CENTER_PAGE_SIZE;
			$this->pagination->initialize($config);
			
			$data['list'] = $msg['list'];
			//$data['no_read'] = $no_read_count;
			$data['title'] = "站内信".' - '.CON_KEYWORDS;
            $data['tab_first'] = 'interact';
            $data['tab_second'] = 'interact_siteMsg';
			$this->load->view('user/user_message_list',$data);
		}else{
			header('Location: /login/index/index.html');
		}
	}
	
	function sendbox(){
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$page = intval($this->input->get('per_page'));;
			$user_id = $user['user_id'];
			$data = array();
			$data = $this->get_user_info('message_yfs','message','invite');

			$this->load->model('Message_model', 'message');
			$msg = $this->message->send_box($user_id,$page);
			$this->load->library('pagination');
			$config['base_url'] = '/message/received.html?';
			$config['total_rows'] = $msg['count'];
			$config['per_page'] = USER_CENTER_PAGE_SIZE;
			//$config['num_links'] = 1;
			$this->pagination->initialize($config);
			$data['list'] = $msg['list'];
			//$data['total'] = $msg['count'];
			$data['title'] = "站内信".' - '.CON_KEYWORDS;
            $data['tab_first'] = 'interact';
            $data['tab_second'] = 'interact_siteMsg';
			$this->load->view('user/user_message_sendbox',$data);
		}else{
			header('Location: /login/index/index.html');
		}
	}
	
	function doread(){
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$user_id = $user['user_id'];
			$ids = $this->input->post('id');
			$fstatus = $this->input->post('status');
			$fstatus = $fstatus ? 1 : 0;
			if(!isset($ids[0])){
				$ret['msg'] = '请先选择';
				echo json_encode($ret);
				exit;
			}
			foreach($ids as $k=>$v){
				$ids[$k] = intval($v);
			}
			
			$this->load->model('Message_model', 'message');
			$this->message->read_message($ids,$user_id,$fstatus);
			$ret = array(
				'msg' => '',
				'data' => array(
					'url' => '',
				),
			);
			echo json_encode($ret);
			
		}else{
			header('Location: /login/index/index.html');
		}
	}
	
	
	
	function dodel(){
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$user_id = $user['user_id'];
			$ids = $this->input->post('id');
			$is_receive = $this->input->post('is_receive');
			if(!$ids){
				$ret['msg'] = '请先选择';
				echo json_encode($ret);
				exit;
			}
			if(is_array($ids)){
				foreach($ids as $k=>$v){
					$ids[$k] = intval($v);
				}
			}else{
				$ids = array(intval($ids));
			}
			
			$this->load->model('Message_model', 'message');
			$this->message->del_message($ids,$user_id,$is_receive);
			$ret = array(
				'msg' => '',
				'data' => array(
					'url' => '',
				),
			);
			echo json_encode($ret);
			
		}else{
			header('Location: /login/index/index.html');
		}
	}
	
	function view($id){
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$data = $this->get_user_info('','received','invite');
			
			//$data = array('msg'=>'');
			$data['msg'] = '';
			
			$user_id = $user['user_id'];
			$id = intval($id);
			$this->load->model('Message_model', 'message');
			$info = $this->message->get_one(array('id'=>$id));
			if($info){
				if($info['sent_user']==$user_id){
					$this->load->model('User_model', 'user');
					$other_uid = $info['receive_user'] ? $info['receive_user'] : 1;
					$other_user = $this->user->get_by_uid($other_uid);
					$info['receive_username'] = $other_user['username'];
					$info['is_receive'] = 0;
				}elseif($info['receive_user']==$user_id){
					$this->load->model('User_model', 'user');
					$this->message->read_message(array($id),$user_id,1);
					$other_uid = $info['sent_user'] ? $info['sent_user'] : 1;
					$other_user = $this->user->get_by_uid($other_uid);
					$info['sent_username'] = $other_user['username'];
					$info['is_receive'] = 1;
				}else{
					//echo "非法操作";
					$data['msg'] = '非法操作';
					$data['url'] = '/message/received/index.html';
				}
			}else{
				$data['msg'] = '非法操作';
				$data['url'] = '/message/received/index.html';
			}
			
			
			$data['info'] = $info;
			$data['title'] = "站内信".' - '.CON_KEYWORDS;
			$this->load->view('user/user_message_view',$data);
			
		}else{
			header('Location: /login');
		}
	}
	
	function sent(){
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$data = $this->get_user_info('message_send','message','invite');
			//$data['user'] = $user;  这里只是session中的用户信息，覆盖了已经获取的用户信息
			$data['title'] = "站内信".' - '.CON_KEYWORDS;
            $data['tab_first'] = 'interact';
            $data['tab_second'] = 'interact_siteMsg';
			$this->load->view('user/user_message_sent',$data);
		}else{
			header('Location: /login/index/index.html');
		}
	}
	
	
	function dosent(){
		$ret = array(
			'msg' => '',
			'data' => array('url'=>''),
		);
		$user = $this->session->userdata('zjy_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$this->load->model('User_model', 'user');
			$this->load->model('Message_model', 'message');
			$receive_user = $this->input->post('receive_user');
			$name = $this->input->post('name');
			$content = $this->input->post('content');
			$err = '';
			if(!$name){
				$err = '标题不能为空';
			}elseif(!$content){
				$err = '内容不能为空';
			}elseif(!$receive_user){
				$receive_username = $this->input->post('receive_username');
				if(!$receive_username){
					$err = '收件人不明确';
				}else{
					$row = $this->user->get_one(array('username'=>$receive_username),'user_id');
					if($row){
						$receive_user = $row['user_id'];
						$receive_info = $this->user->get_by_uid($receive_user);
						if($receive_info['privacy']){
							$receive_info['privacy'] = json_decode($receive_info['privacy'],true);
							if($receive_info['privacy']['message'] < 2 ){
								$err = '收件人拒绝接收';
							}
						}
					}else{
						$err = '收件人不存在';
					}
				}
			}else{
				$receive_info = $this->user->get_by_uid($receive_user);
				if(!$receive_info){
					$err = '收件人不存在';
				}else{
					if($receive_info['privacy']){
						$receive_info['privacy'] = json_decode($receive_info['privacy'],true);
						if($receive_info['privacy']['message'] < 2 ){
							$err = '收件人拒绝接收';
						}
					}
				}
			}
			
			if(!$err && $receive_user < 2){
				$err = '不能给系统管理员发信';
			}
			
			if($err){
				$ret['msg'] = $err;
				echo json_encode($ret);
				exit;
			}
			
			$message = array(
				'sent_user' => $user['user_id'],
				'receive_user' => $receive_user,
				'name' => $name,
				'fstatus' => 0,
				'message_type' => '',
				'sented' => '1',
				'deltype' => '0',
				'content' => $content,
				'add_time' => time(),
				'addip' => $this->input->ip_address(),
			);
			
			$id = $this->message->send($message);
			if($id>0){
				echo json_encode($ret);
				exit;
			}else{
				$ret['msg'] = '发送失败';
				echo json_encode($ret);
				exit;
			}
			
			
		}else{
			header('Location: /login');
		}
	}
}
